<?php

namespace core\process;

use core\gateway\AbstractGateway;
use core\HttpSession;
use core\execute\method\MethodExecute;
use core\execute\method\Method;
use core\Object;
class ProcessView implements Object{

	private static $TAG_INPUT 	= "input";
	private static $TAG_SELECT 	= "select";

	private static $ATTR_VALUE 	= "value";
	private static $ATTR_NAME 	= "name";

	private static $ATTR_GET 	 	= "get";
	private static $ATTR_DISPLAY 	= "display";
	private static $ATTR_DISABLED 	= "disabled";

	private $httpSession;
	private $gateway;
	private $fileHtml;
	private $document;

	public function __construct(HttpSession $httpSession, AbstractGateway $gateway, $fileHtml){
		$this->httpSession = $httpSession;
		$this->gateway 	= $gateway;
		$this->fileHtml = $fileHtml;
		$this->createView();
	}

	private function createView(){
		$this->document = new \DOMDocument(null, null);
		$this->document->loadHTMLFile($this->fileHtml);
		$this->processDOMNode($this->document);
		print_r($this->document->saveHTML());
	}

	private function processDOMNode(\DOMNode $node, \DOMNode $nodeSup = null){
		if($node instanceof \DOMElement){
			$isNode = true;
			if($node->hasAttribute(static::$ATTR_DISPLAY)){
				
			}

			if($isNode){
				if($node->hasAttribute(static::$ATTR_DISABLED)){
					
				}

				if($node->hasAttribute(static::$ATTR_GET)){
					$this->setValueDOMElement($node, $this->getValueObject($this->gateway, $node->getAttribute(static::$ATTR_GET)));
				}
			}
		}

		if($node->hasChildNodes()){
			foreach ($node->childNodes as $newNode){
				$this->processDOMNode($newNode, $node);
			}
		}
	}

	private function setValueDOMElement(\DOMElement $element, $value){
		if(in_array($element->tagName, array(static::$TAG_INPUT))){
			$element->setAttribute(static::$ATTR_VALUE, $value);
		} else {
			$element->nodeValue = $value;
		}
	}

	private function getValueObject(Object $gateway, $methodParam){
		$methods = explode("->", $methodParam);
		foreach ($methods as $key => $name){
			if($key == (count($methods) - 1)){
				return utf8_encode(MethodExecute::execute(Method::createMethod($gateway, $name, array(), true)));
			} else {
				$gateway = MethodExecute::execute(Method::createMethod($gateway, $name, array(), true));
			}
		}
	}
}
?>